package com.td.erhome.ui.sellhouse;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.CiyiInfoBean;
import com.td.erhome.models.HouseArea;
import com.td.erhome.models.HouseIdBean;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;
import com.td.erhome.utils.DialogUtils;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.T;
import com.td.erhome.utils.TextUtil;
import com.td.erhome.utils.UserManager;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class FirstActivity extends BaseActivity{
    @Bind(R.id.img_sure)
    ImageView mImgSure;
    @Bind(R.id.lin_hx)
    LinearLayout mLinhx;
    @Bind(R.id.tv_hx)
    TextView mTvHx;
    @Bind(R.id.et_house_name)
    EditText mEtHouseName;
    @Bind(R.id.et_acreage)
    EditText mEtAcreage;
    @Bind(R.id.et_price)
    EditText mEtPrice;
    @Bind(R.id.et_name)
    EditText mEtName;
    @Bind(R.id.et_phone)
    TextView mEtPhone;
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.tv_xqdz)
    TextView mTvXqdz;
    @Bind(R.id.tv_jd)
    TextView mTvJd;
    @Bind(R.id.progressBar1)
    ProgressBar progressBar1;
    private Subscription mSubscribe;
    private String mS;
    private String type_of_rooms;
    private String type_of_hall;
    private String type_of_guards;
    private long mArea_id;
    private DialogUtils mDialogUtils;
    private Subscription mSubscribe2;
    private CiyiInfoBean mCiyiInfoBean;

    private int  per = 0; //完善度

    @Override
    protected int getLayoutId() {
        return R.layout.activity_first;
    }

    @Override
    protected void initView() {
        mEtPhone.setText(UserManager.getIns().getUser().mobile);
        mEtName.setText(UserManager.getIns().getUser().true_name);

        per = TextUtil.isSpace(UserManager.getIns().getUser().true_name)? 5:10;

        mTvJd.setText("完善度"+per+"%");
        progressBar1.setProgress(per);
        //房屋户型
        mSubscribe = RxBus.getInstance().toObserverable(HouseArea.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HouseArea>() {
                    @Override
                    public void call(HouseArea houseArea) {
                        type_of_rooms = houseArea.type_of_rooms.substring(0,houseArea.type_of_rooms.length()-1);
                        type_of_hall = houseArea.type_of_hall.substring(0,houseArea.type_of_hall.length()-1);
                        type_of_guards = houseArea.type_of_guards.substring(0,houseArea.type_of_guards.length()-1);
                    }
                });

         //地址选择
        mSubscribe2 = RxBus.getInstance().toObserverable(CiyiInfoBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CiyiInfoBean>() {
                    @Override
                    public void call(CiyiInfoBean ciyiInfoBean) {
                        mCiyiInfoBean = ciyiInfoBean;
                        mTvXqdz.setText(ciyiInfoBean.aeraLitePal.area_name+"   "+ciyiInfoBean.streetLitePal.area_name);
                        mArea_id = ciyiInfoBean.streetLitePal.getArea_id();
                    }
                });


        mEtHouseName.addTextChangedListener(new AddWatcher());
        mTvXqdz.addTextChangedListener(new AddWatcher());
        mTvHx.addTextChangedListener(new AddWatcher());
        mEtAcreage.addTextChangedListener(new AddWatcher());
        mEtPrice.addTextChangedListener(new AddWatcher());
        mEtName.addTextChangedListener(new AddWatcher());



        mDialogUtils = new DialogUtils();
    }


    private class AddWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            per=5;
            if (!TextUtil.isEditTextEmpty(mEtHouseName)){
                per+=5;
            }
            if (!TextUtil.isTextViewEmpty(mTvXqdz)){
                per+=5;
            }if (!TextUtil.isTextViewEmpty(mTvHx)){
                per+=5;
            }
            if (!TextUtil.isEditTextEmpty(mEtAcreage)){
                per+=5;
            } if (!TextUtil.isEditTextEmpty(mEtPrice)){
                per+=5;
            } if (!TextUtil.isEditTextEmpty(mEtName)){
                per+=5;
            }
            progressBar1.setProgress(per);
            mTvJd.setText("完善度"+per+"%");

        }
    }

    @Override
    protected void releaseResource() {
        if (!mSubscribe.isUnsubscribed()) {
            mSubscribe.unsubscribe();
        }if (!mSubscribe2.isUnsubscribed()) {
            mSubscribe2.unsubscribe();
        }
        DialogUtils.mPosition = 0;
        DialogUtils.mAreaPoint = 0;
        mDialogUtils = null;
    }

    @OnClick({R.id.lin_hx, R.id.img_sure,R.id.back,R.id.tv_xqdz})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.lin_hx:
                    mDialogUtils.createHomeBottom(this, mTvHx);
                    break;
                case R.id.img_sure:
                    addSellHouseStep1();
                    break;
                case R.id.back:
                    finish();
                    break;
                case R.id.tv_xqdz:
                    mDialogUtils.createAddressBottom(this,mTvXqdz);
                    break;
            }
    }

    //发布买房 第一步

    public void addSellHouseStep1() {


        if (TextUtil.isEditTextEmpty(mEtHouseName)){
            T.showLong(this,"请输入小区名称");
            return;
        }
        if (TextUtil.isTextViewEmpty(mTvXqdz)){
            T.showLong(this,"请选择小区地址");
            return;
        }
        if (TextUtil.isTextViewEmpty(mTvHx)){
            T.showLong(this,"请选择房屋户型");
            return;
        }

        if (TextUtil.isTextViewEmpty(mEtAcreage)){
            T.showLong(this,"请输入房屋面积");
            return;
        }
        if (TextUtil.isEditTextEmpty(mEtPrice)){
            T.showLong(this,"请输入房屋售价");
            return;
        }
        if (TextUtil.isEditTextEmpty(mEtName)){
            T.showLong(this,"请输入屋主姓名");
            return;
        }if (TextUtil.isTextViewEmpty(mEtPhone)){
            T.showLong(this,"请输入联系电话");
            return;
        }

        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", UserManager.getIns().getUser().uid+"");
        params.put("community_name", mEtHouseName.getText().toString());
        params.put("street_id",mArea_id);
        params.put("house_area", mEtAcreage.getText().toString());
        params.put("house_price", mEtPrice.getText().toString());
        params.put("user_name", mEtName.getText().toString());
        params.put("user_tel", mEtPhone.getText().toString());
        params.put("type_of_rooms", type_of_rooms);
        params.put("type_of_hall", type_of_hall);
        params.put("type_of_guards", type_of_guards);

        Api.getRetrofit().addSellHouseStep1(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<HouseIdBean>>(FirstActivity.this){
                    @Override
                    public void onNext(BaseBean<HouseIdBean> houseIdBeanBaseBean) {
                        super.onNext(houseIdBeanBaseBean);
                        Intent intent = new Intent(FirstActivity.this, ContinueActivity.class);
                        intent.putExtra("house_id",houseIdBeanBaseBean.getData().house_id);
                        startActivity(intent);
                        UserManager.getIns().saveUserPer(per);
                        finish();
                    }
                });
    }

}
